package br.unesp.demac.ccn07.aula4.jogo;

import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;

public class UIView extends JFrame implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4400024881450821925L;
	
	private static String sArquivo[] = {
	      "Novo","new16.gif","N",
	      "Salvar","save16.gif","S",
	      null, null, null, 
	      "Sair","blank16.gif","r"};
	
	public UIView () {
		super("Batalha naval");
		JMenuBar mb = new JMenuBar();
		MenuHandler mh = new MenuHandler();
		mb.add(criaMenu("Arquivo", 'A', sArquivo, mh));
		setJMenuBar(mb);
		JMenu menu = mb.getMenu(1);
		
		getContentPane().setLayout(new GridLayout(6,6));
		for(int i = 0; i < 36; i++) {
			MyButton b = new MyButton(i + 1);
			b.addActionListener(this);
			getContentPane().add(b);
		}

		setSize(300, 300);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}
	
   private JMenu criaMenu(String nome,int acc, String itens[],
	         ActionListener al) {
	      JMenu menu = new JMenu(nome); menu.setMnemonic(acc);
	      JMenuItem mi;
	      for (int i=0; i<itens.length; i+=3) {
	         if (itens[i]!=null) {
	            if (itens[i+1]!=null) {
	               ImageIcon icon = new ImageIcon(getClass().getResource("images/"+ itens[i+1]));
               mi = new JMenuItem(itens[i], icon);
            } else { mi = new JMenuItem(itens[i]); }
            if (itens[i+2]!=null) { mi.setMnemonic(itens[i+2].charAt(0)); }
            mi.addActionListener(al);
            menu.add(mi);
         } else { menu.addSeparator(); }
      }
      return menu;
   }
	   
   private class MenuHandler implements ActionListener {
      public void actionPerformed(ActionEvent e) {
         String acao = ((JMenuItem)e.getSource()).getText();
         System.out.printf("%s\n", acao);
         if (acao.equals("Sair")) {
            System.exit(0);
         } 
      } 
   }
   
	public void actionPerformed(ActionEvent event) {
		MyButton bt = (MyButton)event.getSource();
		
		System.out.printf("%d\n", bt.getPosition());
		
	}
   
}

